package com.arvato.right.controller;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arvato.common.util.ContantFinalUtil;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_dto.common.TreeBean;
import com.arvato.minipro.controller.BaseController;
import com.arvato.right.bean.bean_do.WxworkRightResourceDo;
import com.arvato.right.bean.bean_vo.WxworkRightResourceVo;
import com.arvato.right.service.WxworkRightResourceService;
@Controller
@RequestMapping("/admin/right/resource")
public class RightResourceController extends BaseController{
	
	@Autowired
	private WxworkRightResourceService wxworkRightResourceService;

	/**
	 * 获取资源列表
	 * @param param
	 * param 中的参数是 start
	 * 					limit
	 * 					keyword
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = {RequestMethod.POST})
	public PageData listResource(@RequestParam Map<String, Object> param){
		Query query = new Query(param);
		PageData pageData = wxworkRightResourceService.listResourceByQuery(query, query.getPageNum(), query.getPageSize());
		if(query.get("roleId") != null){
			// 如果含有角色ID参数，那么当前角色所拥有的资源，要被打上checked
			List<WxworkRightResourceVo> resourceList = wxworkRightResourceService.listResourceByRoleId(query.get("roleId") + "");
			// 对比角色所拥有的集合和所有集合 将重复的打上checked
			List<?> dataList = pageData.getData();
			for(WxworkRightResourceVo resource : resourceList){
				for(Object data : dataList){
					WxworkRightResourceVo resourceVo = (WxworkRightResourceVo)data;
					if(resourceVo.getResourceId().equals(resource.getResourceId())){
						resourceVo.setChecked(true);
						break;
					}
				}
			}
		}
		return pageData;
	}
	
	/**
	 * 获取资源列表
	 * @param resourceId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", method = {RequestMethod.POST})
	public Result get(String resourceId){
		WxworkRightResourceVo resource = wxworkRightResourceService.getResouceById(resourceId);
		return Result.success(resource);
	}
	
	/**
	 * 请求资源树形结构 生成资源树
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tree")
	public Result tree(){
		List<TreeBean> treeList = wxworkRightResourceService.getResourceTree("0");
		return Result.success(treeList);
	}
	
	/**
	 * 新增资源
	 * @param resourcDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/insert", method = {RequestMethod.POST})
	public Object insert(WxworkRightResourceDo resourcDo){
		resourcDo.setStatus(1);
		if(resourcDo.getParentId().equals("0")){
			resourcDo.setPath(resourcDo.getResourceId());
		}else{
			WxworkRightResourceVo resourceVo = wxworkRightResourceService.getResouceById(resourcDo.getParentId());
			resourcDo.setPath(resourceVo.getPath() + "," + resourcDo.getResourceId());
		}
		try {
			int row = wxworkRightResourceService.insert(resourcDo);
			if(row == 1){
				return Result.success();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("新增失败");
	}
	
	/**
	 * 更新资源状态
	 * @param resource
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = {RequestMethod.POST})
	public Result updateResource(WxworkRightResourceDo resource){
		try {
			int row = wxworkRightResourceService.update(resource);
			if(row == 1){
				return Result.success("更新成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("更新失败");
	}
	
	/**
	 * 删除资源
	 * @param resource
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = {RequestMethod.POST})
	public Result delete(String resourceId){
		if(ContantFinalUtil.MAX_RESOURCE_ID.equals(resourceId)) {
			return Result.fail("最父级资源不可删除。");
		}
		try {
			int row = wxworkRightResourceService.delete(resourceId);
			if(row == 1){
				return Result.success("删除成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Result.fail("删除失败");
	}
	
}
